<template>
	<view class="equity">
		<!-- 这个地方需要判断 -->
			<template v-if="pageParams.list && pageParams.list.length">
				<headerTop  v-if="productType == 'TCL'"   @refrash="getAllList" :pageParams="pageParams"></headerTop>
				<duanLang :orderNo="orderData.params.condition.orderNo"   v-if="productType == 'DuanLangZhongWen'"   @refrash="getAllList" :pageParams="pageParams"></duanLang>
			</template>
		<view class="bottom-list">
			<view class="b-list-title">
				<up-line color="#a3a3a3" length="50rpx" margin="0rpx 20rpx"></up-line>
				<text>优惠券领取</text>
				<up-line margin="0rpx 20rpx" color="#a3a3a3" length="50rpx"></up-line>
			</view>
			<receiveList :pageParams="pageParams" @getTiket="getTiket"></receiveList>
		</view>
		<up-picker
			:show="show"
			@cancel="handleClose"
			:title="pageParams.pickerTitle"
			:columns="pageParams.columns"
			keyName="equityName"
			@confirm="confirm"
			@change="changeHandler"
		></up-picker>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted, watchEffect, watch, computed, defineAsyncComponent } from 'vue';
import { onLoad, onPageScroll, onReady } from '@dcloudio/uni-app';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import { useEquityStore } from '@/store/equity.js';
import initApi from '@/apis/init.js';
import { useUserStore } from '@/store/user.js';
const headerTop = defineAsyncComponent(() => import('./equity/header.vue'));
const duanLang  = defineAsyncComponent(() => import('./equity/duanLangHeader.vue'))
const receiveList = defineAsyncComponent(() => import('./equity/receiveList.vue'));
// 或者使用配置参数来初始化，详情见文档
const { token } = storeToRefs(useUserStore());
const dataR = ref();

const show = ref(false);
const { userInfo } = storeToRefs(useEquityStore());
const pageParams = reactive({
	list: [],
	columns: [],
	equtyId: '',
	pickerTitle: ''
});
const orderData = reactive({
	params: {
		pageNum: 1,
		pageSize: 10000,
		condition: {
			userId: null,
			orderNo: null
		},
		orders: ['updated_time desc']
	},
});
const productType = ref()
onLoad((query) => {
	console.log(query, 'query-- 看看参数都有一些什么',);
	if(query.productType){
		productType.value = query.productType
	}else{
		productType.value = null
	}
	if (query && query.orderNo) {
		orderData.params.condition.orderNo = query.orderNo;
	}
	if (query && query.productType) {
		// 展示可以领取的东西
		productType.value = query.productType;
	}
	getAllList();
	getUserInfo();
});

// 状态数据

const confirm = (e) => {
	const { value } = e;
	console.log(value);
	show.value = false;
	initApi
		.getReceiveEquity({ ...value[0], id: pageParams.equtyId })
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				getAllList();
				return uni.utils.toast('领取成功');
			} else {
				return uni.utils.toast(res.message);
			}
		})
		.catch((error) => {
			return uni.utils.toast(error.message);
		});
};
const handleClose = () => {
	show.value = false;
};
const changeHandler = (e) => {
	console.log(e, 'change');
};

const getTiket = (item) => {
	pageParams.equtyId = item.id;
	if (!item.equityPortfolioConfigList || item.equityPortfolioConfigList?.length < 1) {
		confirNoList(item);
		return;
	}
	// 弹框 没做判断
	if (item.equityType == 1) {
		pageParams.pickerTitle = '选择银行立减金';
	} else if (item.equityType == 2) {
		pageParams.pickerTitle = '选择虚拟会员';
	}

	
	show.value = true;
	pageParams.columns.length = 0;
	pageParams.columns.push(item.equityPortfolioConfigList);
};
const confirNoList = (item) => {
	initApi
		.getReceiveEquity({ id: pageParams.equtyId,packageCode: item.packageCode, annualPayNum: item.annualPayNum  })
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				getAllList();
				return uni.utils.toast('领取成功');
			} else {
				return uni.utils.toast(res.message);
			}
		})
		.catch((error) => {
			return uni.utils.toast(error.message);
		});
};

const userLogin = () => {
	let local = 'https://dev.h5.zzqz2024.com/pages/lhhy/equity'; //获取当前页面地址作为回调地址
	let appid = 'wx5c9c265e473a0aef';
	// 通过微信官方接口获取code之后，会重新刷新设置的回调地址【redirect_uri】
	window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo#wechat_redirect`;
};
const getToken = (code) => {
	initApi
		.userLogin({
			appId: 'wx5c9c265e473a0aef',
			code: code
		})
		.then((res) => {
			dataR.value = res;
			if (res.code == 20000) {
				token.value = res.data.accessToken;
				getAllList();
				getUserInfo();
			}
		})
		.catch((e) => {
			console.log(e, '报错');
		});
};
const getUserInfo = () => {
	initApi
		.getUserInfo()
		.then((res) => {
			console.log(res);
			if (res.code == 20000) {
				userInfo.value.mobile = res.data.mobile;
				userInfo.value.username = res.data.username;
			}
		})
		.catch((error) => {});
};
const getAllList = () => {
	// 获取page
	initApi.getReceiveEquityPage(orderData.params).then((res) => {
		if (res.code == 20000) {
			pageParams.list = res.data.records;
		}
	});
};
</script>

<style lang="scss" scoped>
@import url('@/static/scss/equity.scss');

.bottom-list {
	.b-list-title {
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		// background-color: red;
	}
}

::v-deep {
	image {
		vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
	}
}
</style>
